************************************************
* Sleep Project - Pedro Bessone, Gautam Rao, Heather Schofield, Frank Schilbach, and Mattie Toma
* Purpose: Replicates Appendix Table 4 (Survey of Experts: Summary Statistics)
* Last edited: 07 May 2021
************************************************
clear all
set more off

	use "$d/survey_of_experts.dta", clear

* Flip sign of physical health
replace bp_norm = - bp_norm


************
* SURVEY OF EXPERTS FINAL TABLE
************

* Changing labels for the table
gen _survey_type = .
/// 0: General, 1: Medical, 2: Behavioral
/// Order: General, Behavioral, Medical

replace _survey_type = 0 if survey_type==0 //General first
replace _survey_type = 1 if survey_type==2 //Behavioral second
replace _survey_type = 2 if survey_type==1 //Medical third

drop survey_type
rename _survey_type survey_type



foreach var in correct_entries_norm labor_supply_norm mistakes_norm savings_norm attention_pvt_index pb_ate bp_norm pb_control {

sum `var', de

local mean_`var' = string(r(mean),"%3.2f")

local median_`var' = string(r(p50),"%3.2f")

local p25_`var' = string(r(p25),"%3.2f")

local p75_`var' = string(r(p75),"%3.2f")

local N_`var' = string(r(N),"%3.0f")

}

forval i = 0/2{
foreach var in correct_entries_norm labor_supply_norm mistakes_norm savings_norm attention_pvt_index pb_ate bp_norm pb_control {

sum `var' if survey_type==`i', de

local mean_`var'_`i' = string(r(mean),"%3.2f")

local median_`var'_`i' = string(r(p50),"%3.2f")

local p25_`var'_`i' = string(r(p25),"%3.2f")

local p75_`var'_`i' = string(r(p75),"%3.2f")

* We use a conditional statement so we print a "." when there is no observation for a given expert type
if r(N) == 0{
local N_`var'_`i' = "."
}
else{
local N_`var'_`i' = string(r(N),"%3.0f")
}
}
}

			cd "$oa/Tables"
			file open f using "TableA4_survey_experts.tex", write replace
				file write f "\begin{tabular}{l*{20}{c}}" _n ///+
				"\toprule" _n ///
				"&\multicolumn{5}{c}{\textbf{All}}&\multicolumn{5}{c}{\textbf{General Economists}}&\multicolumn{5}{c}{\textbf{Behavioral Economists}}&\multicolumn{5}{c}{\textbf{Sleep Experts}}\\" _n ///
				"\cmidrule(lr){2-6}\cmidrule(lr){7-11}\cmidrule(lr){12-16}\cmidrule(lr){17-21} &\multicolumn{1}{c}{Mean}    &\multicolumn{1}{c}{p25}     &\multicolumn{1}{c}{Median}    &\multicolumn{1}{c}{p75}  &\multicolumn{1}{c}{N}    &\multicolumn{1}{c}{Mean}   &\multicolumn{1}{c}{p25}  &\multicolumn{1}{c}{Median} &\multicolumn{1}{c}{p75} &\multicolumn{1}{c}{N}  &\multicolumn{1}{c}{Mean}&\multicolumn{1}{c}{p25} &\multicolumn{1}{c}{Median} &\multicolumn{1}{c}{p75}    &\multicolumn{1}{c}{N}  &\multicolumn{1}{c}{Mean}  &\multicolumn{1}{c}{p25} &\multicolumn{1}{c}{Median} &\multicolumn{1}{c}{p75} &\multicolumn{1}{c}{N}  \\" _n ///
				"&\multicolumn{1}{c}{(1)}    &\multicolumn{1}{c}{(2)}     &\multicolumn{1}{c}{(3)}    &\multicolumn{1}{c}{(4)}     &\multicolumn{1}{c}{(5)}   &\multicolumn{1}{c}{(6)}  &\multicolumn{1}{c}{(7)} &\multicolumn{1}{c}{(8)}&\multicolumn{1}{c}{(9)}&\multicolumn{1}{c}{(10)} &\multicolumn{1}{c}{(11)} &\multicolumn{1}{c}{(12)}    &\multicolumn{1}{c}{(13)}  &\multicolumn{1}{c}{(14)} &\multicolumn{1}{c}{(15)} &\multicolumn{1}{c}{(16)}  &\multicolumn{1}{c}{(17)}  &\multicolumn{1}{c}{(18)} &\multicolumn{1}{c}{(19)} &\multicolumn{1}{c}{(20)}  \\" _n ///
				"\midrule" _n ///
				"Correct Entries & `mean_correct_entries_norm' &  `p25_correct_entries_norm' & `median_correct_entries_norm' & `p75_correct_entries_norm' & `N_correct_entries_norm' & `mean_correct_entries_norm_0' &  `p25_correct_entries_norm_0' & `median_correct_entries_norm_0' & `p75_correct_entries_norm_0' & `N_correct_entries_norm_0' & `mean_correct_entries_norm_1' &  `p25_correct_entries_norm_1' & `median_correct_entries_norm_1' & `p75_correct_entries_norm_1' & `N_correct_entries_norm_1' & `mean_correct_entries_norm_2' &  `p25_correct_entries_norm_2' & `median_correct_entries_norm_2' & `p75_correct_entries_norm_2' & `N_correct_entries_norm_2' \\ \\" _n ///
				"Hours Working  & `mean_labor_supply_norm' &  `p25_labor_supply_norm' & `median_labor_supply_norm' &`p75_labor_supply_norm' & `N_labor_supply_norm' & `mean_labor_supply_norm_0' &  `p25_labor_supply_norm_0' &  `median_labor_supply_norm_0' & `p75_labor_supply_norm_0' & `N_labor_supply_norm_0' & `mean_labor_supply_norm_1' &  `p25_labor_supply_norm_1' & `median_labor_supply_norm_1' & `p75_labor_supply_norm_1' & `N_labor_supply_norm_1' & `mean_labor_supply_norm_2' & `p25_labor_supply_norm_2' & `median_labor_supply_norm_2' &  `p75_labor_supply_norm_2' & `N_labor_supply_norm_2' \\ \\" _n ///
				"Savings & `mean_savings_norm' &  `p25_savings_norm' & `median_savings_norm' &`p75_savings_norm' & `N_savings_norm' & `mean_savings_norm_0' &  `p25_savings_norm_0' & `median_savings_norm_0' & `p75_savings_norm_0' & `N_savings_norm_0' & `mean_savings_norm_1' &  `p25_savings_norm_1' & `median_savings_norm_1' & `p75_savings_norm_1' & `N_savings_norm_1' & `mean_savings_norm_2' &  `p25_savings_norm_2' & `median_savings_norm_2' &`p75_savings_norm_2' & `N_savings_norm_2' \\ \\" _n ///
				"Present Bias & `mean_pb_ate' &  `p25_pb_ate' & `median_pb_ate' &`p75_pb_ate' & `N_pb_ate' & `mean_pb_ate_0' &  `p25_pb_ate_0' & `median_pb_ate_0' & `p75_pb_ate_0' & `N_pb_ate_0' & `mean_pb_ate_1' & `p25_pb_ate_1' & `median_pb_ate_1' & `p75_pb_ate_1' & `N_pb_ate_1' & `mean_pb_ate_2' & `p25_pb_ate_2' & `median_pb_ate_2' &  `p75_pb_ate_2' & `N_pb_ate_2' \\ \\" _n ///
				"Attention & `mean_attention_pvt_index' &  `p25_attention_pvt_index' & `median_attention_pvt_index' & `p75_attention_pvt_index' & `N_attention_pvt_index' & `mean_attention_pvt_index_0' &  `p25_attention_pvt_index_0' & `median_attention_pvt_index_0' &`p75_attention_pvt_index_0' & `N_attention_pvt_index_0' & `mean_attention_pvt_index_1' &  `p25_attention_pvt_index_1' & `median_attention_pvt_index_1' &`p75_attention_pvt_index_1' & `N_attention_pvt_index_1' & `mean_attention_pvt_index_2' &  `p25_attention_pvt_index_2' & `median_attention_pvt_index_2' &`p75_attention_pvt_index_2' & `N_attention_pvt_index_2' \\ \\" _n ///
				"Physical Health & `mean_bp_norm' &  `p25_bp_norm' & `median_bp_norm' & `p75_bp_norm' & `N_bp_norm' & `mean_bp_norm_0' &`p25_bp_norm_0' &  `median_bp_norm_0' & `p75_bp_norm_0' & `N_bp_norm_0' & `mean_bp_norm_1' & `p25_bp_norm_1' &  `median_bp_norm_1' & `p75_bp_norm_1' & `N_bp_norm_1' & `mean_bp_norm_2' & `p25_bp_norm_2' & `median_bp_norm_2' & `p75_bp_norm_2' & `N_bp_norm_2' \\ \\" _n ///
				"\bottomrule" _n ///
				"\end{tabular}" _n
			file close f
	
